﻿using System;
using System.Collections.Generic;

namespace JX.Infrastructure.Common
{
	/// <summary>
	/// 缓存接口
	/// </summary>
	public interface ICacheService
    {
		/// <summary>
		/// 验证缓存项是否存在
		/// </summary>
		/// <param name="key">缓存Key</param>
		/// <returns></returns>
		bool Exists(string key);

		#region 得到缓存
		/// <summary>
		/// 获取缓存
		/// </summary>
		/// <typeparam name="T">获取缓存对象类型</typeparam>
		/// <param name="key">缓存Key</param>
		/// <returns>获取指定key对应的值</returns>
		T Get<T>(string key) where T : class;
		/// <summary>
		/// 获取缓存
		/// </summary>
		/// <param name="key">缓存Key</param>
		/// <returns>获取指定key对应的值</returns>
		object Get(string key);
		/// <summary>
		/// 获取缓存集合
		/// </summary>
		/// <param name="keys">缓存Key集合</param>
		/// <returns></returns>
		IDictionary<string, object> GetAll(IEnumerable<string> keys);
		#endregion

		#region 添加缓存
		/// <summary>
		/// 添加缓存,如果存在则更新
		/// </summary>
		/// <param name="key">缓存Key</param>
		/// <param name="value">缓存Value</param>
		/// <returns>是否添加成功</returns>
		bool AddOrUpdate(string key, object value);
		/// <summary>
		/// 添加缓存,如果存在则更新
		/// </summary>
		/// <param name="key">缓存Key</param>
		/// <param name="value">缓存Value</param>
		/// <param name="expiresSliding">滑动过期时长（如果在过期时间内有操作，则以当前时间点延长过期时间）</param>
		/// <param name="expiressAbsoulte">绝对过期时长</param>
		/// <returns>是否添加成功</returns>
		bool AddOrUpdate(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte);
		/// <summary>
		/// 添加缓存,如果存在则更新
		/// </summary>
		/// <param name="key">缓存Key</param>
		/// <param name="value">缓存Value</param>
		/// <param name="expiresIn">缓存时长</param>
		/// <param name="isSliding">是否滑动过期（如果在过期时间内有操作，则以当前时间点延长过期时间）</param>
		/// <returns>是否添加成功</returns>
		bool AddOrUpdate(string key, object value, TimeSpan expiresIn, bool isSliding = false);
		#endregion

		#region 删除缓存
		/// <summary>
		/// 删除缓存
		/// </summary>
		/// <param name="key">缓存Key</param>
		/// <returns></returns>
		bool Remove(string key);
		/// <summary>
		/// 删除所有缓存
		/// </summary>
		void RemoveAll();
		/// <summary>
		/// 批量删除缓存
		/// </summary>
		/// <param name="keys">缓存Key集合</param>
		void RemoveAll(IEnumerable<string> keys);
		/// <summary>
		/// 删除匹配到的缓存
		/// </summary>
		/// <param name="pattern">正则表达式</param>
		void RemoveCacheRegex(string pattern);
		#endregion

		#region 其他
		/// <summary>
		/// 获取所有缓存键
		/// </summary>
		/// <returns></returns>
		List<string> GetCacheKeys();
		/// <summary>
		/// 搜索 匹配到的缓存键
		/// </summary>
		/// <param name="pattern">正则表达式</param>
		/// <returns></returns>
		IList<string> SearchCacheRegex(string pattern);
		#endregion
	}
}
